<template>
  <div>
    <el-card class="box-card">
      <el-table border :data="skuList" style="width: 100%; margin: 10px 0px">
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column prop="skuName" show-overflow-tooltip label="名称" />
        <el-table-column prop="skuDesc" show-overflow-tooltip label="描述" />
        <el-table-column prop="city" label="图片">
          <template #="{ row, $index }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
        <el-table-column prop="weight" label="重量" />
        <el-table-column prop="price" label="价格" />
        <el-table-column fixed="right" label="操作" width="200">
          <template #="{ row, $index }">
            <el-button
              v-if="row.isSale == 0"
              type="success"
              size="small"
              icon="Top"
              @click="ChangeSaleState(row)"
              circle
            />
            <el-button
              v-else
              type="info"
              size="small"
              icon="Bottom"
              circle
              @click="ChangeSaleState(row)"
            />
            <el-button
              type="primary"
              size="small"
              icon="Edit"
              circle
              @click="EditSku(row)"
            />
            <el-button
              type="warning"
              size="small"
              icon="InfoFilled"
              @click="showDrawer(row)"
              circle
            />
            <el-popconfirm
              :title="`真的要删除${row.skuName}?`"
              width="200px"
              @confirm="deleteSku(row)"
            >
              <template #reference>
                <el-button type="danger" size="small" icon="Delete" circle />
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        @size-change="handleSizeChange"
        @current-change="getSkuPageData"
        :page-sizes="[10, 20, 30, 40]"
        background="true"
        layout="prev, pager, next, jumper, ->, sizes, total"
        :total="total"
      />
    </el-card>
    <!--左侧弹出框-->
    <el-drawer v-model="drawer">
      <template #header>
        <h4>查看商品详情</h4>
      </template>
      <template #default>
        <div>
          <el-row style="margin: 15px 0px">
            <el-col :span="6">销售属性</el-col>
            <el-col :span="18">{{ skuInfo?.skuName }}</el-col>
          </el-row>
          <el-row style="margin: 15px 0px">
            <el-col :span="6">销售属性</el-col>
            <el-col :span="18">{{ skuInfo?.skuDesc }}</el-col>
          </el-row>
          <el-row style="margin: 15px 0px">
            <el-col :span="6">价格</el-col>
            <el-col :span="18">{{ skuInfo?.price }}</el-col>
          </el-row>
          <el-row style="margin: 15px 0px">
            <el-col :span="6">平台属性</el-col>
            <el-col :span="18">
              <el-tag
                style="margin: 5px 5px"
                v-for="(item, index) in skuInfo?.skuAttrValueList"
                :key="item.id"
                type="primary"
              >
                {{ item.valueName }}
              </el-tag>
            </el-col>
          </el-row>
          <el-row style="margin: 15px 0px">
            <el-col :span="6">销售属性</el-col>
            <el-col :span="18">
              <el-tag
                style="margin: 5px 5px"
                v-for="(item, index) in skuInfo?.skuSaleAttrValueList"
                :key="item.id"
                type="primary"
              >
                {{ item.saleAttrValueName }}
              </el-tag>
            </el-col>
          </el-row>
          <el-row style="margin: 15px 0px">
            <el-col :span="6">商品图片</el-col>
            <el-col :span="18">
              <el-carousel :interval="4000" type="card" height="200px">
                <el-carousel-item
                  v-for="item in skuInfo?.skuImageList"
                  :key="item.id"
                >
                  <img :src="item.imgUrl" style="width: 100%; height: 100%" />
                </el-carousel-item>
              </el-carousel>
            </el-col>
          </el-row>
        </div>
      </template>
    </el-drawer>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
//引入sku相关api
import {
  GetSkuPageData,
  OnSale,
  CancelSale,
  GetSkuInfoBySkuID,
  DeleteSkuByID,
} from '@/api/product/sku/index'
//引入sku相关类型
import type {
  SkuResponsePageData,
  SkuRecord,
  SkuResponseInfoData,
} from '@/api/product/sku/type'
import { ElMessage } from 'element-plus'
//当前页
let currentPage = ref<number>(1)
//每页显示条数
let pageSize = ref<number>(10)
//总页数
let total = ref<number>(0)
//收集sku列表数据
let skuList = ref<SkuRecord[]>([])
//控制左侧弹出框的显示与隐藏
let drawer = ref<boolean>(false)
//sku信息
let skuInfo = ref<SkuRecord>()
//获取sku分页数据
const getSkuPageData = async (pager: number = 1) => {
  currentPage.value = pager
  const result: SkuResponsePageData = await GetSkuPageData(
    currentPage.value,
    pageSize.value,
  )
  if (result.code == 200) {
    total.value = result.data.total
    skuList.value = result.data.records
  }
}
//分页器每页条数发生变化时
const handleSizeChange = (pagesize: number) => {
  pageSize.value = pagesize
  getSkuPageData()
}
//处理SKU上架或者下架
const ChangeSaleState = async (row: SkuRecord) => {
  if (row.isSale == 0) {
    //表示现在是下架状态，要进行上架操作
    await OnSale(row.id as number)
    ElMessage.success('上架成功')
    getSkuPageData(currentPage.value)
  } else {
    await CancelSale(row.id as number)
    ElMessage.success('下架成功')
    getSkuPageData(currentPage.value)
  }
}
//SKU编辑
const EditSku = (row: SkuRecord) => {
  ElMessage.success('程序员正在加紧处理中....')
}
//显示左侧弹出框
const showDrawer = async (row: SkuRecord) => {
  drawer.value = true
  //获取sku信息
  let result = await GetSkuInfoBySkuID(row.id as number)
  if (result.code == 200) {
    skuInfo.value = result.data
  } else {
    ElMessage.error(result.message)
  }
}

const deleteSku = async (row: SkuRecord) => {
  let result = await DeleteSkuByID(row.id as number)
  if (result.code == 200) {
    ElMessage.success('删除成功')
    getSkuPageData(
      skuList.value.length > 1 ? currentPage.value : currentPage.value - 1,
    )
  } else {
    ElMessage.error(result.data)
  }
}
//页面加载完成时调用
onMounted(() => {
  getSkuPageData()
})
</script>

<style scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
